#!/usr/bin/env bash

if [[ "$(id -un)" != "root" ]]; then
    echo "请使用 sudo 执行此脚本"
    exit 1
fi

M=$(dmidecode -s system-manufacturer | awk '{$1=$1};1')
P=$(dmidecode -s system-product-name | awk '{$1=$1};1')
V=$(dmidecode -s system-version | awk '{$1=$1};1')
F=$(dmidecode -s system-family | awk '{$1=$1};1')

model="$F-$P"
if [[ "$F" == "Default string" && "$V" == "Default string" ]]; then 
    model=$P
else
    model="$V"
    if [[ "$V" == "Standard" || "$V" == "Not Specified" ]]; then
        model=$P
    else
        if [[ "${P}" == *"${V}"* ]]; then
            model=$P
        else
            if [[ "${F}" == *"${M}"* ]]; then
                model=$F
            else
                if [[ "${F}" != *"${V}"* ]]; then
                    model=$F-$V
                fi
            fi
        fi
    fi
fi

machine=$M/$model

if [[ "$P" == "" && "$S" == "" ]]; then 
    machine=$1
fi

if [[ "$machine" == "" ]]; then
    echo "archive failed, path prefix is empty"
    exit 1
fi

echo "您的计算机型号是: $machine"

if [ -d "deviceInfo" ]; then
    mkdir -p "archives/$machine"
    cp -R deviceInfo/* "archives/$machine"
    chown 1000:1000 -R "archives"
fi


